package com.yogurt.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yogurt.edu.client.VodClient;
import com.yogurt.edu.entity.Video;
import com.yogurt.edu.mapper.VideoMapper;
import com.yogurt.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author yogurt
 * @since 2020-07-10
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    //根据课程id删除小节
    // TODO 根据视频id删除对应的视频
    @Override
    public void deleteVideoByCourseId(String courseId) {
        //构建条件
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        //只查询video_source_id这一列
        queryWrapper.select("video_source_id");
        //查询出所有的Video对象
        List<Video> videoList = baseMapper.selectList(queryWrapper);

        List<String> list = new ArrayList<>();
        //循环遍历videoList，得到每一个video对象,从而得到每一个video对象的视频id
        for (Video video : videoList) {
            String videoSourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                list.add(videoSourceId);
            }
        }

        if (list.size() > 0){
            vodClient.deleteBatchVideo(list);
        }


        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
